import java.util.Arrays;

public class TestSort {

    //归并排序
    private static void merge(int[] array, int low, int mid, int high) {
        int s1 = low;
        int e1 = mid;
        int s2 = mid+1;
        int e2 = high;

    }

    private static void mergeSortInternal(int[] array, int low, int high) {
        if (low >= high) return;
        int mid = low + ((high-low)>>>1);
        mergeSortInternal(array, low, mid);
        mergeSortInternal(array, mid+1, high);
        merge(array, low, mid, high);
    }

    public static void mergeSort(int[] array) {
        mergeSortInternal(array, 0, array.length-1);
    }

    //快速排序
    public static int partitionHoare(int[] array, int low, int high) {
        int i = low;
        int tmp = array[low];
        while (low < high) {
            while (low < high && array[high] >= tmp) {
                high--;
            }
            while (low < high && array[high] <= tmp) {
                low++;
            }
            swap(array, low, high);
        }
        swap(array, low, i);
        return low;
    }

    //挖坑法
    public static int partitionHole(int[] array, int low, int high) {
        int tmp = array[low];
        while (low < high) {
            while (low < high && array[high] >= tmp) {
                high--;
            }
            array[low] = array[high];
            while (low < high && array[low] <= tmp) {
                low++;
            }
            array[high] = array[low];
        }
        array[low] = tmp;
        return low;
    }

    //前后指针法
    public static int partition(int[] array, int low, int high) {
        int prev = low;
        int cur = prev+1;
        while (cur <= high) {
            if (array[cur] < array[low] && array[++prev] != array[cur]) {
                swap(array, cur, prev);
            }
            cur++;
        }
        swap(array, prev, low);
        return prev;
    }

    public static void quick(int[] array, int left, int right) {
        if (left >= right) return;
        int pivot = partition(array, left, right);
        quick(array, left, pivot-1);
        quick(array, left+1, right);
    }

    public static void quickSort(int[] array) {
        quick(array,0,array.length-1);
    }

    //冒泡排序
    /**
     * 时间复杂度：O(N^2)
     * 空间复杂度：O(1)
     * 稳定性：稳定
     * @param array
     */
    public static void bubbleSort(int[] array) {
        for (int i = 0; i < array.length-1; i++) {
            for (int j = 0; j < array.length-1-i; j++) {
                if (array[j] > array[j+1]) {
                    swap(array, j, j+1);
                }
            }
        }
    }

    public static void bubbleSort2(int[] array) {
        for (int i = 0; i < array.length-1; i++) {
            boolean flg = false;
            for (int j = 0; j < array.length-1-i; j++) {
                if (array[j] > array[j+1]) {
                    swap(array, j, j+1);
                    flg = true;
                }
            }
            if (!flg) {
                break;
            }
        }
    }

    //堆排序
    /**
     * 时间复杂度：O(n*logn)
     * 空间复杂度：O(1)
     * 稳定性：不稳定
     * @param array
     */
    public static void heapSort(int[] array) {

    }

    //直接选择排序
    /**
     * 时间复杂度：O(n^2)
     * 空间复杂度：O(1)
     * 稳定性：不稳定
     * @param array
     */
    public static void selectSort(int[] array) {
        for (int i = 0; i < array.length; i++) {
            int minIndex = i;
            for (int j = i+1; j < array.length; j++) {
                if (array[j] < array[minIndex]) {
                    minIndex = j;
                }
            }
            swap(array, i, minIndex);
        }
    }

    public static void swap(int[] array, int i, int j) {
        int tmp = array[i];
        array[i] = array[j];
        array[j] = tmp;
    }

    //希尔排序：缩小增量排序
    public static void shell(int[] array, int gap) {
        for (int i = gap; i < array.length; i++) {
            int tmp = array[i];
            int j = i - gap;
            for (; j >= 0; j -= gap) {
                if (tmp < array[j]) {
                    array[j+gap] = array[j];
                } else {
                    break;
                }
            }
            array[j+gap] = tmp;
        }
    }

    public static void shellSort(int[] array) {
        int gap = array.length;
        while (gap > 1) {
            shell(array, gap);
            gap /= 2;
        }
        shell(array, 1);
    }

    //直接插入排序
    public static void insertSort(int[] array) {
        for (int i = 1; i < array.length; i++) {
            int tmp = array[i];
            int j = i-1;
            for (; j >= 0; j--) {
                if (tmp < array[j]) {
                    array[j+1] = array[j];
                } else {
                    //array[j+1] = tmp;
                    break;
                }
            }
            array[j+1] = tmp;
        }
    }

    public static void main(String[] args) {
        int[] array = {5, 10, 3, 8, 2};
        System.out.println("排序前：" + Arrays.toString(array));
        //insertSort(array);
        quickSort(array);
        System.out.println("排序后：" + Arrays.toString(array));
    }
}
